package com.tusuo.stds.ui.vip;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tusuo.stds.Constants;
import com.tusuo.stds.R;
import com.tusuo.stds.rest.UserInfo;
import com.tusuo.stds.rest.VipList;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.pay.MyPayActivity;
import com.tusuo.stds.ui.pay.MyPayActivity_;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by developer on 17-8-29.
 */

@EActivity(R.layout.activity_vip)
public class VipActivity extends BaseToolbarActivity<VipView,VipPresenter> implements VipView {

    @ViewById(R.id.vip_layout_normal)
    LinearLayout layoutNormal;
    @ViewById(R.id.vip_layout_special)
    LinearLayout layoutSpecial;
    @ViewById(R.id.vip_layout_background)
    LinearLayout layoutBackGround;
    @ViewById(R.id.vip_include_pay)
    LinearLayout layoutPay;
    @ViewById(R.id.vip_layout_price_group)
    LinearLayout layoutGroup;
    @ViewById(R.id.vip_layout_main)
    RelativeLayout layoutMain;
    @ViewById(R.id.vip_layout_hint)
    RelativeLayout layoutHint;

    @ViewById(R.id.vip_tv_date_expire)
    TextView tvExpire;
    @ViewById(R.id.vip_tv_name)
    TextView tvName;
    @ViewById(R.id.vip_tv_loading)
    TextView tvLoading;

    @ViewById(R.id.vip_btn_pay)
    Button btnPay;

    @ViewById(R.id.vip_iv_privilege)
    ImageView ivPrivilege;
    @ViewById(R.id.vip_layout_iv_close)
    ImageView ivClose;
    @ViewById(R.id.vip_civ_icon)
    CircleImageView civIcon;

    @ViewById(R.id.vip_lv_list)
    ListView lvList;

    private TranslateAnimation animation_show;
    private TranslateAnimation animation_hide;
    private boolean isShowAnimationFinish = true;
    private boolean isHideAnimationFinish = true;
    private ArrayList<VipList.VipItem> mVipData;
    private VipPresenter mPresenter;


    @NonNull
    @Override
    public VipPresenter createPresenter() {
        mPresenter = new VipPresenter(this);
        return mPresenter;
    }

    @AfterViews
    void initData() {
        setupUI(this,getResources().getColor(R.color.white),getResources().getColor(R.color.black));
        ToolbarOwner.Config config = new ToolbarOwner.Config("会员中心", true, R.drawable.ic_back_dark, null);
        mToolbarOwner.setConfig(config);
        initAnimation();
    }

    void initAnimation() {
        animation_show = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 1,
                Animation.RELATIVE_TO_PARENT, 0);

        animation_show.setDuration(500);
        animation_show.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                isShowAnimationFinish = false;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isShowAnimationFinish = true;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


        animation_hide = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 0,
                Animation.RELATIVE_TO_PARENT, 1);

        animation_hide.setDuration(500);
        animation_hide.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                isHideAnimationFinish = false;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isHideAnimationFinish = true;
                layoutPay.setVisibility(View.GONE);
                layoutHint.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }

    @Override
    public void initVipData(UserInfo userInfo, VipList vipList) {

        mVipData = vipList.Data;
        if (userInfo.VipLevel == 0) {
            // 不是VIP
            layoutNormal.setVisibility(View.VISIBLE);
            layoutSpecial.setVisibility(View.GONE);
            btnPay.setText("开通会员");
        }else {
            // 是VIP
            layoutNormal.setVisibility(View.GONE);
            layoutSpecial.setVisibility(View.VISIBLE);
            SimpleDateFormat sdfServer = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            SimpleDateFormat sdfApp = new SimpleDateFormat("yyyy-MM-dd");
            Date vipTime;
            try {
                vipTime = sdfServer.parse(userInfo.VipTime);
            } catch (ParseException e) {
                e.printStackTrace();
                vipTime = new Date(4100688000L);
            }
            tvExpire.setText(sdfApp.format(vipTime));
            btnPay.setText("续费会员");
        }

        tvName.setText(userInfo.Name);

        if (TextUtils.isEmpty(userInfo.Photo)) {
            Glide
                    .with(this)
                    .load(R.drawable.img_avatar_default)
                    .centerCrop()
                    .into(civIcon);
        } else {
            Glide
                    .with(this)
                    .load(Constants.ENDPOINT + userInfo.Photo)
                    .centerCrop()
                    .into(civIcon);
        }



        tvLoading.setVisibility(View.GONE);
        layoutMain.setVisibility(View.VISIBLE);

        lvList.setAdapter(new VipAdapter(this));

        if (layoutGroup.getChildCount() < 1) {
            initVipPay();
        }
    }

    private void initVipPay() {
        for (final VipList.VipItem item : mVipData) {
            View view = View.inflate(this,R.layout.item_vip_price,null);
            TextView tvMonth = (TextView) view.findViewById(R.id.item_vip_tv_month);
            TextView tvPrice = (TextView) view.findViewById(R.id.item_vip_tv_price);
            tvMonth.setText(item.Title);
            tvPrice.setText(String.format(Locale.SIMPLIFIED_CHINESE, "%.0f", item.Price));

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    hidePayLayout();
                    layoutPay.setVisibility(View.INVISIBLE);
                    Intent intent = new Intent();
                    intent.setClass(VipActivity.this, MyPayActivity_.class);
                    intent.putExtra("payType","vip");
                    intent.putExtra("vipId",item.Id);
                    intent.putExtra("vipMonth",item.Title);
                    intent.putExtra("vipPrice",item.Price);
                    startActivity(intent);
                }
            });

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);
            int px = Utils.dp2px(15, this);
            params.setMargins(0,0,px,0);

            layoutGroup.addView(view,params);
        }

        isDataReady = true;

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (tvLoading != null && layoutMain != null) {
            tvLoading.setVisibility(View.VISIBLE);
            layoutMain.setVisibility(View.INVISIBLE);
        }

        if (layoutPay != null && layoutHint != null) {
            layoutPay.setVisibility(View.GONE);
            layoutHint.setVisibility(View.GONE);

        }
        mPresenter.initUserInfo();
    }

    @Click({R.id.vip_layout_iv_close,R.id.vip_layout_hint})
    void hidePayLayout() {
        if (layoutPay.getVisibility() == View.VISIBLE && isShowAnimationFinish && isHideAnimationFinish) {
            layoutPay.startAnimation(animation_hide);
        }
    }

    private boolean isDataReady = true;

    @Click(R.id.vip_btn_pay)
    void payForVip() {

        if (mVipData == null || mVipData.size() == 0) {
            Utils.UI.showToastShort("未获取到VIP支付信息，请重试");
            if (isDataReady) {
                presenter.getVipList();
                isDataReady = false;
            }
            return;
        }

        if (layoutPay.getVisibility() != View.VISIBLE && isShowAnimationFinish && isHideAnimationFinish) {
            layoutPay.setVisibility(View.VISIBLE);
            layoutHint.setVisibility(View.VISIBLE);
            layoutPay.startAnimation(animation_show);
        }
    }

    @ItemClick(R.id.vip_lv_list)
    void toPrivilege() {
        Intent intent = new Intent(this,PrivilegeActivity_.class);
        startActivity(intent);
    }
}
